<template>
  <div class="m-3 gap-2 flex">
    <p class="font-bold">订单号:</p>{{ orderId }}
  </div>

  <div>
    <uni-section title="打分" subTitle="一星为差评，两星为一般，三星为好评" type="line" padding>
      <uni-rate :max="3" :value="1" v-model="rate" />
    </uni-section>
  </div>

  <div class="m-4">
    <uni-easyinput type="textarea" v-model="text" placeholder="聊聊感受，您的评价能帮助到其他人~"></uni-easyinput>
  </div>
  <div class="m-3 gap-2 flex flex-col justify-between " style="font-size:large">
    <div>
      <p class="text">上传图片</p>
    </div>
    <div class="m-2">
      <uni-file-picker limit="9" title="最多上传9张图片,请尽量保证图片清晰可见"></uni-file-picker>
    </div>
  </div>
  <div class="justify-between">
    <button type="primary" @touchend="handleComment">
      提交评价
    </button>

  </div>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app';
import { useDocumentsStore } from '../../stores/documents';
import { useUserStore } from '../../stores/user';

const isOwner = ref(false)
const user = useUserStore()
const rate = ref(1)
const text = ref('')
const orderId = ref('')
const workers = ref([])
const order = ref({})
const documents = useDocumentsStore()

onLoad((option) => {
  orderId.value = option.id
  console.log('isOwner:', option.isOwner)
  if (option.isOwner) isOwner.value = true
  order.value = documents.orders.find(v => v._id = option.id)
  workers.value = order.value.workers
})

console.log('onload workers:', workers.value)

function handleComment() {
  console.log(rate.value, text.value)
  if (!isOwner.value) //不是雇主的评论
    uni.request({
      url: '/comment/',
      method: 'POST',
      data: {
        receiver: order.value.owner,
        rate: rate.value,
        content: text.value,
        order: order.value._id
      },
      success: ({ data }) => {
        console.log('data:' + data)
        uni.navigateTo({ url: '/pages/acceptsuccess/acceptsuccess' })
      }
    })
  else { //雇主的评论(批量)
    uni.request({
      url: '/comment/all',
      method: 'POST',
      data: {
        rate: rate.value,
        content: text.value,
        order: order.value._id
      },
      success: ({ data }) => {
        console.log('data:' + data)
        uni.navigateTo({ url: '/pages/acceptsuccess/acceptsuccess' })
      }
    })
  }
}


</script>